<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="姓名"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getUsers">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--列表-->
        <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" label="编号" width="60">
            </el-table-column>
            <el-table-column prop="username" label="姓名" width="120" sortable>
            </el-table-column>
            <el-table-column prop="email" label="邮箱" width="100"  sortable>
            </el-table-column>
            <el-table-column prop="phone" label="电话" width="120" sortable>
            </el-table-column>
            <el-table-column prop="password" label="密码" min-width="180" sortable>
            </el-table-column>
            <el-table-column prop="age" label="年龄" width="100" sortable>
            </el-table-column>
            <el-table-column prop="state" label="状态" :formatter="formatSex" width="100" sortable>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="total, sizes, prev, pager, next, jumper"
                           @current-change="handleCurrentChange"
                           @size-change="handleSizeChange"
                           :current-page="currentPage"
                           :page-size="pageSize"
                           :page-sizes="pageSizes"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增界面-->
        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm"  label-width="80px">
                <el-form-item label="姓名" prop="username">
                    <el-input v-model="addForm.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="addForm.password" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="phone">
                    <el-input v-model="addForm.phone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addForm.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model="addForm.age" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="状态" prop="state">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">在职</el-radio>
                        <el-radio class="radio" :label="0">离职</el-radio>
                    </el-radio-group>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    export default {
        data(){
            return{
                addFormVisible:false,
                users:[],//双向绑定 列表数据
                listLoading: false, //加载框
                sels:[],  //列表选中项
                currentPage:1,//当前页
                pageSize:5,//默认显示每页条数
                pageSizes:[5,10,10], //每页条数
                total:0,//总条数
                filters:{
                    name:''
                },
                addForm:{
                    username:'',
                    password:'',
                    phone:'',
                    email:'',
                    age:'',
                    state:''
                }
            }
        },
        methods:{
            formatSex(row){
                return row.state==1?"在职":"离职"
            },
            //当页码发生改变时 触发
            handleCurrentChange(val){
                this.currentPage = val;
                this.loadEmployee();
            },
            //当每页条数发生变化时 触发
            handleSizeChange(val){
                this.currentPage = 1;
                this.pageSize = val;
                this.loadEmployee();
            },
            selsChange(sels){
                this.sels = sels;
            },
            //批量删除
            batchRemove(){
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/employee/batchDelEmployee",ids).then((res) => {
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.loadEmployee();
                        }else {
                            this.$message.error(res.data.msg)
                        }
                    });
                }).catch(() => {

                });
            },
            //编辑按钮
            handleEdit(index,row){
                this.addFormVisible = true;
                console.debug("编辑按钮",row);
                //回显
                for(var k in row){
                    this.addForm[k] = row[k]
                }
            },
            //删除按钮
            handleDel(index,row){
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.delete("/employee/"+row.id).then((res) => {
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.loadEmployee();
                        }else {
                            this.$message.error(res.data.msg)
                        }
                    });
                }).catch(() => {

                });
            },
            //高级查询 按钮
            getUsers(){
                this.loadEmployee();
            },
            //添加 按钮
            handleAdd(row){
                this.addFormVisible = true;
                for(var k in this.addForm){
                    this.addForm[k] = "";
                }
            },
            //加载员工列表
            loadEmployee(){
                let partm = {
                    "currentPage":this.currentPage,
                    "pageSize":this.pageSize,
                    "username":this.filters.name
                };
                this.$http.patch("/employee",partm).then((res => {
                    this.users = res.data.list;
                    this.total = res.data.total;
                    console.debug("加载员工列表",res);
                }));
            },
            //添加 修改 模态框确认按钮
            addSubmit(){
                let parp = Object.assign({},this.addForm);
                console.debug("修改确认按钮",this.addForm.id);
                if(this.addForm.id){
                    this.$http.post("/employee",parp).then((res => {
                        if(res.data.success){
                            this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                            this.addFormVisible=false;
                            this.loadEmployee();
                        }else {
                            this.$message.error(res.data.msg);
                        }
                    }));
                }else {
                    this.$http.put("/employee",parp).then((res => {
                        if(res.data.success){
                            this.$message({
                                message: '添加成功',
                                type: 'success',
                            });
                            this.addFormVisible = false;
                            this.loadEmployee()
                        }else {
                            this.$message.error(res.data.msg);
                        }
                    }));
                }
            }
        },
        mounted(){
            this.loadEmployee();
        }
    }
</script>